package animation.sort.algorithms;

import java.util.Random;

import animation.sort.algorithms.QuickSort;

/**
 * Created by IntelliJ IDEA.
 * User: emilio
 * Date: Aug 12, 2004
 * Time: 2:07:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class RandomQuickSort
        extends QuickSort
{
    void sort(int array[], int low, int high)
            throws Exception
    {
        if (low < high) {
            markBox(low, high);

            int pivot = pickPivot(array, low, high);
            swap(array, pivot, high);
            int pos = partition(array, low, high);
            sort(array, low, pos - 1);
            sort(array, pos + 1, high);
        }
    }

    int pickPivot(int[] array, int low, int high)
            throws Exception
    {
        doSleep(3000);
        return random.nextInt(high + 1 - low) + low;
    }

    public String toString()
    {
        return "QSort (Random)";
    }


    private Random random = new Random();
}
